<?php
/*
*	$version: 2009-5-19 12:33:39 beta 1.0
*	$id: memcache.php 
*	$author: taoFeng
*	$mailto: font99@Gmail.com
*	$license: This is not a freeware
*/

if (!defined('IN_LEKO')) {header("HTTP/1.0 403 Forbidden");exit;}

include ('Storage.class'.$_G['phpEx']);

class mysql4 extends Storage
{
	private $runSql = '<br />';
	private $dbHost;
	public $dbUser;
	private $dbPersistency;
	private $dbName;
	private $dbLink;
	private $queryResult;

	public function getNumQueries() {
	}

	public function fetchRowset($_queryid = false) {
	}

	public function stoageInit($_sqlserver, $_sqluser, $_sqlpassword, $_dbname, $_port = false, $_persistency = false) {

		$this->dbPersistency = $_persistency;
		$this->dbUser = $_sqluser;
		$this->dbHost = $_sqlserver . (($_port) ? ':' . $_port : '');
		$this->dbName = $_dbname;
		
		$this->dbLink = ($this->dbPersistency) ? @mysql_pconnect($this->dbHost, $this->dbUser, $sqlpassword) : @mysql_connect($this->server, $this->dbUser, $_sqlpassword);

		$dbCharset = str_replace('-', '', $GLOBALS['_G']['charSet']);
		if(mysql_get_server_info($this->dbLink) > '4.1' && $dbCharset) {
			mysql_query("SET character_set_connection=".$dbCharset.", character_set_results=".$dbCharset.",character_set_client=binary",$this->dbLink);
		}
		if(mysql_get_server_info($this->dbLink) > '5.0') {
			mysql_query("SET sql_mode=''",$this->dbLink);
		}

		if ($this->dbLink && $this->dbName != '') {
			@mysql_select_db($this->dbName);
		}

		return ($this->dbLink) ? true : false;
	}

	public function Query($_query = '',$_type = '') {
		$queryResult = false;

		if ($_query != '') {
			$func = $_type == 'UNBUF' && @function_exists('mysql_unbuffered_query') ?
			'mysql_unbuffered_query' : 'mysql_query';
			
			if ($this->dbLink) {
				if (!($queryResult = $func($_query, $this->dbLink))) {
					$err = $this->dbError();
					if ($err['code'] == '2006' || $err['code'] == '2013' && strncasecmp($type,"TRY",3) != 0) {
						$this->dbClose();
						require FILE_ROOT . 'config/config.inc' . $GLOBALS['_G']['phpEx'];
						$this->stoageInit($GLOBALS['_G']['dbHost'], $GLOBALS['_G']['dbUser'], $GLOBALS['_G']['dbPass'], $GLOBALS['_G']['dbName'], $GLOBALS['_G']['dbPort'], false);
						$this->queryResult = $this->Query($sql, 'TRY'.$type);
					}

					return false;
				}

				$this->runSql .= $_query . "<br />";

				$this->num_queries++;
			}
		}
		else {
			return false;
		}

		$this->queryResult = $queryResult;

		return $queryResult;
	}

	public function getNumrows($_queryid = null) {
		return ($_queryid) ? mysql_num_rows($_queryid) : false;
	}

	public function getAffectedrows() {
		return ($this->dbLink) ? mysql_affected_rows($this->dbLink) : false;
	}

	public function fetchRow($_queryid = false,$_resulttype = MYSQL_ASSOC) {

		if (!$_queryid) {
			$_queryid = $this->queryResult;
		}
		return ($_queryid) ? @mysql_fetch_array($_queryid,$_resulttype) : false;
	}

	public function getRunSql() {
			return $this->runSql;
	}

	public function fetchField($_field, $_rownum = false, $_queryid = false) {

		if (!$_queryid) {
			$_queryid = $this->queryResult;
		}

		if ($_queryid) {

			if ($_rownum === false) {

				$row = $this->fetchRow($_queryid);
				return isset($row[$_field]) ? $row[$_field] : false;
			}
			else {
				return @mysql_result($_queryid, $_rownum, $_field);
			}
		}

		return false;
	}

	public function rowSeek($_rownum, $_queryid = false) {

		if (!$_queryid) {
			$_queryid = $this->queryResult;
		}

		return ($_queryid) ? @mysql_data_seek($_queryid, $_rownum) : false;
	}

	public function nextId() {
		return ($this->dbLink) ? @mysql_insert_id($this->dbLink) : false;
	}

	public function freeResult($_queryid = false) {
		if (!$_queryid) {
			$_queryid = $this->queryResult;
			return @mysql_free_result($_queryid);
		}

		return false;
	}

	public function Escape($_string) {
		return mysql_escape_string($_string);
	}

	public function dbError()
	{
		return array(
			'message'	=> @mysql_error(),
			'code'		=> @mysql_errno()
		);
	}

	public function dbClose()
	{
		return @mysql_close($this->dbLink);
	}

}

?>
